package com.hzz.service.eybase;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzz.api.eybase.IEyBaseRetrieveChnGmService;
import com.hzz.dao.eybase.IEyBaseRetrieveChnGmDao;
import com.hzz.dto.eybase.EyBaseRetrieveGmHydlDto;
import com.hzz.entity.eybase.EyBaseRetrieveGmHydl;
import com.hzz.framework.Response;
import com.hzz.framework.SysErrorCode;
import com.hzz.framework.annotation.FeignService;
import com.hzz.framework.exception.ServiceException;
import com.hzz.framework.service.BaseService;
import com.hzz.framework.util.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@FeignService
@Slf4j
public class EyBaseRetrieveChnGmService extends BaseService implements IEyBaseRetrieveChnGmService {
    @Autowired
    IEyBaseRetrieveChnGmDao eyBaseRetrieveChnGmDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {Exception.class, RuntimeException.class})
    public Response saveOrUpdateData(EyBaseRetrieveGmHydlDto dto) throws Exception {
        Response result = new Response(0, "success");
        try {
            EyBaseRetrieveGmHydl entity = copyTo(dto, EyBaseRetrieveGmHydl.class);
            //判断数据是否存在
            if (eyBaseRetrieveChnGmDao.isDataExist(entity) != 0) {
                //数据存在
                eyBaseRetrieveChnGmDao.update(entity);
            } else {
                //新增
                eyBaseRetrieveChnGmDao.insert(entity);
            }
        } catch (Exception e) {
            log.error("信息保存异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    public String deleteData(EyBaseRetrieveGmHydlDto dto) throws Exception {
        String result = "success";
        try {
            EyBaseRetrieveGmHydl entity = copyTo(dto, EyBaseRetrieveGmHydl.class);
            if (eyBaseRetrieveChnGmDao.deleteByPrimaryKey(entity) == 0) {
                throw new RuntimeException("数据不存在!");
            }
        } catch (Exception e) {
            log.error("物理删除异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }


    @Override
    public PageInfo findDataIsPage(EyBaseRetrieveGmHydlDto dto) throws Exception {
        PageInfo pageInfo = null;
        try {
            EyBaseRetrieveGmHydl entity = copyTo(dto, EyBaseRetrieveGmHydl.class);
            PageHelper.startPage(PN(dto.getPageNum()), PS(dto.getPageSize()));
            List list = eyBaseRetrieveChnGmDao.findDataIsPage(entity);
            pageInfo = new PageInfo(list);
            pageInfo.setList(copyTo(pageInfo.getList(), EyBaseRetrieveGmHydlDto.class));
        } catch (Exception e) {
            log.error("信息[分页]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return pageInfo;
    }

    @Override
    public List<EyBaseRetrieveGmHydlDto> findDataIsList(EyBaseRetrieveGmHydlDto dto) throws Exception {
        List<EyBaseRetrieveGmHydlDto> results = null;
        try {
            EyBaseRetrieveGmHydl entity = copyTo(dto, EyBaseRetrieveGmHydl.class);
            results = copyTo(eyBaseRetrieveChnGmDao.findDataIsList(entity), EyBaseRetrieveGmHydlDto.class);
        } catch (Exception e) {
            log.error("信息[列表]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return results;
    }

    @Override
    public EyBaseRetrieveGmHydlDto findDataById(EyBaseRetrieveGmHydlDto dto) throws Exception {
        EyBaseRetrieveGmHydlDto result = null;
        try {
            EyBaseRetrieveGmHydl entity = copyTo(dto, EyBaseRetrieveGmHydl.class);
            result = copyTo(eyBaseRetrieveChnGmDao.selectByPrimaryKey(entity), EyBaseRetrieveGmHydlDto.class);
        } catch (Exception e) {
            log.error("信息[详情]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    public int deleteByExpireDay(Integer expireDay) {
        try {
            return eyBaseRetrieveChnGmDao.deleteByExpireDay(expireDay);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
